﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Net.NetworkInformation;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;

namespace BEO
{
    public partial class Main : PhoneApplicationPage
    {

        public ErrandCollection Errands { get { return (App.Current as App).Errands; } }

        // Constructor
        public Main()
        {
            InitializeComponent();

            ValidateInternetConnection();
        }

        #region Event Handlers

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            lbErrands.SelectedValue = null;  //Remove any existing item highlighting
        }

        private void View_Click(object sender, EventArgs e)
        {
            // get id of errand item
            string id = string.Empty;
            try
            {
                id = (sender as MenuItem).Tag.ToString();
            }
            catch
            {
                id = (sender as StackPanel).Tag.ToString();
            }
            finally
            {
                if (!string.IsNullOrEmpty(id))
                {
                    // navigate
                    this.NavigationService.Navigate(
                        new Uri(
                            String.Format("/View.xaml?id={0}", id),
                            UriKind.Relative)
                    );
                }
            }
        }

        private void Delete_Click(object sender, EventArgs e)
        {
            // get id of errand item
            Guid id = (Guid)(sender as MenuItem).Tag;

            // remove
            Errands.Remove((from errand in Errands where errand.Id.Equals(id) select errand).First());
        }

        private void Add_Click(object sender, EventArgs e)
        {
            this.NavigationService.Navigate(new Uri("/Add.xaml", UriKind.Relative));
        }

        private void Optimize_Click(object sender, EventArgs e)
        {
            this.NavigationService.Navigate(new Uri("/Optimize.xaml", UriKind.Relative));
        }

        #endregion

        private void Errands_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            lbErrands.ItemsSource = (App.Current as App).Errands;
        }

        private bool ValidateInternetConnection()
        {
            // forcing false until I'm done testing...as emulator is always connected to internet
            bool available = NetworkInterface.GetIsNetworkAvailable();

            if (!available)
            {
                string caption = "Internet Unavailable";
                string text = "This application needs internet access to properly function.  If you do not enable access to the internet, it's not our fault and you agree not to dock our grade for it.\n\nClick ok to open settings, or cancel to exit.";
                MessageBoxResult mbr = MessageBox.Show(text, caption, MessageBoxButton.OKCancel);

                if (mbr.Equals(MessageBoxResult.OK))
                {
                    ConnectionSettingsTask connectionSettingsTask = new ConnectionSettingsTask();
                    connectionSettingsTask.ConnectionSettingsType = ConnectionSettingsType.WiFi;
                    connectionSettingsTask.Show();

                    // just for testing if thread is continuing to run in background
                    System.Threading.Thread.Sleep(5000);
                }
                else
                {
                    throw new ExitException();
                }
            }

            return available;
        }
    }
}